package com.moleq.remote.rpt;

import java.io.StringWriter;

import org.codehaus.jackson.map.ObjectMapper;

public class JsonConvertor {
	private static ObjectMapper  objectMapper = new ObjectMapper(); 

	public JsonConvertor() {
		objectMapper = new ObjectMapper();
	}

	public static SocketRequest toSocketRequest(String json) {
		
//		json = "{\"datetime\":1361943027703,\"formIp\":\"192.168.1.115\",\"object\":\"\",\"method\":\"getResponse\",\"param\":\"2012-12-30\",\"ClassName\":\"Report\",\"params\":[]}";
		SocketRequest request = null;
		try {
			request = objectMapper.readValue(json, SocketRequest.class);
			System.out.println(request.ClassName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return request;
	}

	public static String socketRequest2String(SocketRequest request) {

		String result = "";
		try {
			StringWriter writer = new StringWriter();
			objectMapper.writeValue(writer, request);
			result = writer.toString();

			System.out.println();
			System.out.println("socketRequest2String ->");
			System.out.println(result);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}
	
	public static SocketResponse toSocketResponse(String str){
		SocketResponse response = null;
		try {
			response = objectMapper.readValue(str, SocketResponse.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return response;
	}
	
	public static String socketResponse2String(SocketResponse response){
		String result = "";
		try {
			StringWriter writer = new StringWriter();
			objectMapper.writeValue(writer, response);
			result = writer.toString();

			System.out.println();
			System.out.println("socketResponse2String ->");
			System.out.println(result);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}
}
